home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP08 / BTNLOOK.C next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.1 KB  |  149 lines

  1. /*----------------------------------------
  2.    BTNLOOK.C -- Button Look Program
  3.                 (c) Charles Petzold, 1996
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. struct
  9.      {
  10.      long style ;
  11.      char *text ;
  12.      }
  13.      button[] =
  14.      {
  15.      BS_PUSHBUTTON,      "PUSHBUTTON",
  16.      BS_DEFPUSHBUTTON,   "DEFPUSHBUTTON",
  17.      BS_CHECKBOX,        "CHECKBOX", 
  18.      BS_AUTOCHECKBOX,    "AUTOCHECKBOX",
  19.      BS_RADIOBUTTON,     "RADIOBUTTON",
  20.      BS_3STATE,          "3STATE",
  21.      BS_AUTO3STATE,      "AUTO3STATE",
  22.      BS_GROUPBOX,        "GROUPBOX",
  23.      BS_AUTORADIOBUTTON, "AUTORADIO",
  24.      BS_OWNERDRAW,       "OWNERDRAW"
  25.      } ;
  26.  
  27. #define NUM (sizeof button / sizeof button[0])
  28.  
  29. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  30.  
  31. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  32.                     PSTR szCmdLine, int iCmdShow)
  33.      {
  34.      static char szAppName[] = "BtnLook" ;
  35.      HWND        hwnd ;
  36.      MSG         msg ;
  37.      WNDCLASSEX  wndclass ;
  38.  
  39.      wndclass.cbSize        = sizeof (wndclass) ;
  40.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  41.      wndclass.lpfnWndProc   = WndProc ;
  42.      wndclass.cbClsExtra    = 0 ;
  43.      wndclass.cbWndExtra    = 0 ;
  44.      wndclass.hInstance     = hInstance ;
  45.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  46.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  47.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  48.      wndclass.lpszMenuName  = NULL ;
  49.      wndclass.lpszClassName = szAppName ;
  50.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  51.  
  52.      RegisterClassEx (&wndclass) ;
  53.  
  54.      hwnd = CreateWindow (szAppName, "Button Look",
  55.                           WS_OVERLAPPEDWINDOW,
  56.                           CW_USEDEFAULT, CW_USEDEFAULT,
  57.                           CW_USEDEFAULT, CW_USEDEFAULT,
  58.                           NULL, NULL, hInstance, NULL) ;
  59.  
  60.      ShowWindow (hwnd, iCmdShow) ;
  61.      UpdateWindow (hwnd) ;
  62.  
  63.      while (GetMessage (&msg, NULL, 0, 0))
  64.           {
  65.           TranslateMessage (&msg) ;
  66.           DispatchMessage (&msg) ;
  67.           }
  68.      return msg.wParam ;
  69.      }
  70.  
  71. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  72.      {
  73.      static char  szTop[]    = "iMsg            wParam       lParam",
  74.                   szUnd[]    = "____            ______       ______",
  75.                   szFormat[] = "%-16s%04X-%04X    %04X-%04X",
  76.                   szBuffer[50] ;
  77.      static HWND  hwndButton[NUM] ;
  78.      static RECT  rect ;
  79.      static int   cxChar, cyChar ;
  80.      HDC          hdc ;
  81.      PAINTSTRUCT  ps ;
  82.      int          i ;
  83.      TEXTMETRIC   tm ;
  84.  
  85.      switch (iMsg)
  86.           {
  87.           case WM_CREATE :
  88.                hdc = GetDC (hwnd) ;
  89.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  90.                GetTextMetrics (hdc, &tm) ;
  91.                cxChar = tm.tmAveCharWidth ;
  92.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  93.                ReleaseDC (hwnd, hdc) ;
  94.  
  95.                for (i = 0 ; i < NUM ; i++)
  96.                     hwndButton[i] = CreateWindow ("button", button[i].text,
  97.                               WS_CHILD | WS_VISIBLE | button[i].style,
  98.                               cxChar, cyChar * (1 + 2 * i),
  99.                               20 * cxChar, 7 * cyChar / 4,
  100.                               hwnd, (HMENU) i,
  101.                               ((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
  102.                return 0 ;
  103.  
  104.           case WM_SIZE :
  105.                rect.left   = 24 * cxChar ;
  106.                rect.top    =  2 * cyChar ;
  107.                rect.right  = LOWORD (lParam) ;
  108.                rect.bottom = HIWORD (lParam) ;
  109.                return 0 ;
  110.  
  111.           case WM_PAINT :
  112.                InvalidateRect (hwnd, &rect, TRUE) ;
  113.  
  114.                hdc = BeginPaint (hwnd, &ps) ;
  115.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  116.                SetBkMode (hdc, TRANSPARENT) ;
  117.  
  118.                TextOut (hdc, 24 * cxChar, cyChar, szTop, sizeof (szTop) - 1) ;
  119.                TextOut (hdc, 24 * cxChar, cyChar, szUnd, sizeof (szUnd) - 1) ;
  120.  
  121.                EndPaint (hwnd, &ps) ;
  122.                return 0 ;
  123.  
  124.           case WM_DRAWITEM :
  125.           case WM_COMMAND :
  126.                ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;
  127.  
  128.                hdc = GetDC (hwnd) ;
  129.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  130.  
  131.                TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),
  132.                         szBuffer,
  133.                     wsprintf (szBuffer, szFormat,
  134.                         iMsg == WM_DRAWITEM ? "WM_DRAWITEM" : "WM_COMMAND",
  135.                         HIWORD (wParam), LOWORD (wParam),
  136.                         HIWORD (lParam), LOWORD (lParam))) ;
  137.  
  138.                ReleaseDC (hwnd, hdc) ;
  139.                ValidateRect (hwnd, &rect) ;
  140.  
  141.                break ;
  142.  
  143.           case WM_DESTROY :
  144.                PostQuitMessage (0) ;
  145.                return 0 ;
  146.           }
  147.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  148.      }
  149.